<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class information_controller extends public_controller
{
    public function index()
    {

    }

    public function load_report(){
        $information = $this->information->get_elements_checked_in();

        $this->load->view('report',array('data' => $information));


    }

    public function check_ticket_number(){
        date_default_timezone_set('Asia/Bangkok');

        $ticket_number = $this->input->post('ticket');

        $ticket = $this->information->get_element_by_ticket_number($ticket_number);
        $result = array('status' => 'FAIL','message' => 'Thẻ không hợp lệ, không tìm thấy trong dữ liệu');

        if($ticket->id != ''){

            if($ticket->check_in == '' || $ticket->check_in == '0'){

                $data = array(
                    'id' => $ticket->id,
                    'check_in' => time()
                );
                if($this->information->update_ticket($data)){
                    $result['message'] = 'Thẻ hợp lệ. </br><span style="color:green"> ' . $ticket->ticket_title . '</span>';
                    $result['status'] = 'SUCCESS';
                }
            }else{
                $result['message'] = 'Thẻ <span style="color:green;font-weight:bold">[' . $ticket->ticket_title . ']</span> đã check in lúc ' . date('H:i',$ticket->check_in) . ' ngày  ' . date('d/m/Y',$ticket->check_in);
            }


        }

        $this->check_in_log->log_by_ticket_number($ticket_number);
        $result['rightCardNumber'] = count($this->information->get_elements_checked_in());
        $result['wrongCardNumber'] = $this->check_in_log->get_wrong_number()->sumCount;

        $titles = $this->information->get_all_ticket_type();
        $data = array();
        foreach($titles as $title){
            $data[] = array(
                'ticket_title' => $title->ticket_title,
                'sum_number' => $this->information->get_check_in_count($title->ticket_title)->cnt
            );

        }

        $result['summary'] = $data;

//        $ticket_failed = $this->check_in_log->get_count_wrong_check_in();
//        echo $ticket_failed;exit;

        echo json_encode($result);
    }

}